#include "MarsPrecompile.h"
#include "MarsMeshPlane.h"
#include "MarsInputLayout.h"
#include "MarsVertexData.h"

using namespace Graphics;

typedef struct
{
	XMFLOAT3 Pos;
	XMFLOAT2 TexCoord;
	XMFLOAT3 Normal;
} Vertex;

bool MeshPlane::Load()
{
	InputLayout* pInputLayout = new InputLayout();
	pInputLayout->AddElement( "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 );
	pInputLayout->AddElement( "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, sizeof(XMFLOAT3), D3D11_INPUT_PER_VERTEX_DATA, 0 );
	pInputLayout->AddElement( "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, sizeof(XMFLOAT3) + sizeof(XMFLOAT2), D3D11_INPUT_PER_VERTEX_DATA, 0 );

	Vertex* pVertices = new Vertex[ 4 ];

	Vertex vertices[] =
	{
		{ XMFLOAT3( -5.0f,  0.0f,  5.0f ), XMFLOAT2( 0.0f, 0.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) },
		{ XMFLOAT3(  5.0f,  0.0f,  5.0f ), XMFLOAT2( 1.0f, 0.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) },
		{ XMFLOAT3(  5.0f,  0.0f, -5.0f ), XMFLOAT2( 1.0f, 1.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) },
		{ XMFLOAT3( -5.0f,  0.0f, -5.0f ), XMFLOAT2( 0.0f, 1.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) },
	};

	uint16* pIndices = new uint16[ 6 ];
	uint16 indices[] =
	{
		0,   1,  3,  1,  2,  3
	};

	MARS_MEMCPY( pIndices, indices, sizeof( indices ) );

	MARS_MEMCPY( pVertices, vertices, sizeof( vertices ) );

	m_pVertexData = new VertexData( pInputLayout, ( uint8* ) pVertices, sizeof( Vertex ) * 4, ( uint8* ) pIndices, sizeof( indices ), VertexData::IBF_UInt16 );
	m_eTopology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;

	m_iVertexCount = 6;
	m_iStride = sizeof( Vertex );
	m_iOffset = 0;

	return true;
}
